#include "VentanaAviso.h"
#include "../constantes.h"

VentanaAviso::VentanaAviso(double x, double y, double ancho, double alto, string mensaje):
Redimensionable(x, y, ancho, alto) {
	this->mensaje = mensaje;
	this->renderer = NULL;
	this->window = NULL;
	this->texturaFondo = NULL;

	if (TTF_Init() == -1){
		cout << "ERROR al inicializar TTF!" << endl;
	} else {
		cout << "Se inicializo bien" << endl;
	}

	this->fuente = TTF_OpenFont("C:/WINDOWS/Fonts/Arial.ttf", 13);
	if(this->fuente == NULL){
	  	cout << "PUNTERO NULO!!!" << endl;
	} else {
		cout << "Se inicializo OK la fuente!" << endl;
	}

	SDL_Colour color = {0x00,0x00,0x00};
	this->color_texto = color;
}

VentanaAviso::~VentanaAviso(){
	SDL_DestroyTexture(texturaFondo);
}

bool VentanaAviso::inicializar() {

	if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
		std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
		return false;
	}
	window = SDL_CreateWindow("Ventana", this->getX(), this->getY(),
			this->getAncho(), this->getAlto(),
			SDL_WINDOW_SHOWN);
	if (window == NULL) {
		std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
		return false;
	}
	renderer = SDL_CreateRenderer(window, -1,
			SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if (renderer == NULL) {
		std::cout << "SDL_CreateRenderer Error: " << SDL_GetError()
				<< std::endl;
		return 1;
	}

	return true;

}

void VentanaAviso::renderizarObjetos() {
	SDL_RenderPresent(this->renderer);
}

void VentanaAviso::dibujarTexto() {

	double anchoMensaje = this->mensaje.length() * 5.8;
	double xMensaje = (this->ancho - anchoMensaje) / 2;

	SDL_Surface* surfaceMsg;
	surfaceMsg = TTF_RenderText_Solid(this->fuente, mensaje.c_str(), this->color_texto);
	SDL_Rect rectMsg;
	//rectMsg.x = 55;
	rectMsg.x = xMensaje;
	rectMsg.y = 50;

	SDL_Texture * tex = SDL_CreateTextureFromSurface(renderer, surfaceMsg);
	SDL_QueryTexture(tex, NULL, NULL, &rectMsg.w,&rectMsg.h);
	SDL_RenderCopy(renderer, tex, NULL, &rectMsg);
	SDL_DestroyTexture(tex);
	SDL_FreeSurface(surfaceMsg);

}


void VentanaAviso::dibujar(double x, double y, double ancho, double alto,
		SDL_Renderer* renderer) {

	if (texturaFondo == NULL) {
		texturaFondo = IMG_LoadTexture(renderer, "img/ventana_aviso.png");
		if (texturaFondo == NULL) {
			std::cout << "SDL_CreateTextureFromSurface Error: "
					<< SDL_GetError() << std::endl;
			return;
		}
	}

	SDL_Rect sdlRect;

	sdlRect.x = 0;
	sdlRect.y = 0;
	sdlRect.w = this->ancho;
	sdlRect.h = this->alto;
	SDL_RenderCopy(renderer, texturaFondo, NULL, &sdlRect);

	this->dibujarTexto();

	this->renderizarObjetos();

	return;

}

void VentanaAviso::detectarEventos() {

	SDL_Event evento;
	bool quit = false;

	while(!quit){
		while (SDL_PollEvent(&evento)) {
			if (evento.button.type
					== SDL_MOUSEBUTTONDOWN&& evento.button.button == SDL_BUTTON_LEFT) {

				// Clickeo en el boton OK
				if ((evento.button.x >= BOTON_ACEPTAR_VENTANA_AVISO_X )
						&& (evento.button.x <= BOTON_ACEPTAR_VENTANA_AVISO_X + BOTON_ACEPTAR_VENTANA_AVISO_ANCHO)
						&& (evento.button.y >= BOTON_ACEPTAR_VENTANA_AVISO_Y)
						&& (evento.button.y <= BOTON_ACEPTAR_VENTANA_AVISO_Y + BOTON_ACEPTAR_VENTANA_AVISO_ALTO)) {
					cout << "Clickeo en el boton OK" << endl;
					quit = true;
					return;
				}
			}

			if (evento.type == SDL_QUIT) {
				quit = true;
				return;
			}
		}
	}
}

void VentanaAviso::cerrarVentana(){
	SDL_DestroyWindow(window);
}
